package com.jrx.anytxn.param.service.investor.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jrx.anytxn.common.data.TxnRespCode;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.common.uitl.BeanMapping;
import com.jrx.anytxn.common.uitl.StringUtils;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.number.exception.SegmentException;
import com.jrx.anytxn.number.service.ISegmentService;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.constant.ErrorMsgConstant;
import com.jrx.anytxn.param.dto.investor.GaInvestorSubjectConfigReq;
import com.jrx.anytxn.param.dto.investor.GaInvestorSubjectConfigRes;
import com.jrx.anytxn.param.entity.GaInvestorSubjectConfig;
import com.jrx.anytxn.param.mapper.ext.ExtGaInvestorSubjectConfigMapper;
import com.jrx.anytxn.param.service.investor.IGaInvestorSubjectConfigService;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 出资方科目配置表业务接口实现类
 *
 * @author 黄秋平
 * @create 2020/6/15
 */
@Service
public class GaInvestorSubjectConfigServiceImpl implements IGaInvestorSubjectConfigService {

    private static final Logger logger = LoggerFactory.getLogger(GaInvestorSubjectConfigServiceImpl.class);

    @Resource
    private ExtGaInvestorSubjectConfigMapper extGaInvestorSubjectConfigMapper;

    @Resource
    private ISegmentService segmentService;

    @Override
    public TxnPage<GaInvestorSubjectConfigRes> findAllPageByTenId(Integer pageNum, Integer pageSize, String tenantId) throws TxnException {
        Page<GaInvestorSubjectConfig> page = PageHelper.startPage(pageNum, pageSize, Constant.ORDER_BY_SORT);
        List<GaInvestorSubjectConfig> list = extGaInvestorSubjectConfigMapper.selectByTenantId(tenantId);
        if (list.isEmpty()) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        List<GaInvestorSubjectConfigRes> resList=BeanMapping.copyList(list,GaInvestorSubjectConfigRes.class);
        for (GaInvestorSubjectConfigRes ga:resList) {
            List<GaInvestorSubjectConfig> gaInvestorSubjectConfigList = extGaInvestorSubjectConfigMapper.selectByTenIdAndInvestorIdAndOrgId(tenantId, ga.getInvestorId(),ga.getOrganizationId());
            List<String> subjectIds=new ArrayList<>();
            for (GaInvestorSubjectConfig gaSubjectConfig:gaInvestorSubjectConfigList) {
                subjectIds.add(gaSubjectConfig.getSubjectId());
            }
            ga.setSubjectIds(subjectIds);
        }
        return new TxnPage<>(pageNum, pageSize, page.getTotal(), resList);
    }

    @Override
    public GaInvestorSubjectConfigRes addInvestorTable(GaInvestorSubjectConfigReq gaInvestorSubjectConfigReq) throws TxnException {
        List<GaInvestorSubjectConfig> gaInvestorSubjectConfigList = extGaInvestorSubjectConfigMapper.selectByTenIdAndInvestorIdAndOrgId(gaInvestorSubjectConfigReq.getTenantId(), gaInvestorSubjectConfigReq.getInvestorId(),gaInvestorSubjectConfigReq.getOrganizationId());
        if (CollectionUtils.isNotEmpty(gaInvestorSubjectConfigList)) {
            logger.error(TxnRespCode.DATA_EXISTS.getMsg());
            throw new TxnException(TxnRespCode.DATA_EXISTS.getMsg());
        }
        for (String subjectId:gaInvestorSubjectConfigReq.getSubjectIds()) {
            GaInvestorSubjectConfig investorSubjectConfig = BeanMapping.copy(gaInvestorSubjectConfigReq, GaInvestorSubjectConfig.class);
            investorSubjectConfig.setSubjectId(subjectId);
            try {
                investorSubjectConfig.setId(segmentService.getId(Constant.GA_INVESTOR_SUBJECT_CONFIG));
            } catch (SegmentException e) {
                logger.error("主键生成错误",e);
                throw new TxnException(ErrorMsgConstant.ERROR_ID);
            }
            investorSubjectConfig.setRecordVersionNumber(Constant.VERSION_NUMBER);
            extGaInvestorSubjectConfigMapper.insertSelective(investorSubjectConfig);
        }

        return BeanMapping.copy(gaInvestorSubjectConfigReq, GaInvestorSubjectConfigRes.class);
    }

    @Override
    public GaInvestorSubjectConfigRes modifyInvestorTable(GaInvestorSubjectConfigReq gaInvestorSubjectConfigReq) throws TxnException {
        List<GaInvestorSubjectConfig> gaInvestorSubjectConfigList = extGaInvestorSubjectConfigMapper.selectByTenIdAndInvestorIdAndOrgId(gaInvestorSubjectConfigReq.getTenantId(), gaInvestorSubjectConfigReq.getInvestorId(),gaInvestorSubjectConfigReq.getOrganizationId());
        if (CollectionUtils.isEmpty(gaInvestorSubjectConfigList)) {
            logger.error(TxnRespCode.NO_DATA.getMsg());
            throw new TxnException(TxnRespCode.NO_DATA.getMsg());
        }
        extGaInvestorSubjectConfigMapper.deleteByInvestorIdAndTenId(gaInvestorSubjectConfigReq.getInvestorId(), gaInvestorSubjectConfigReq.getTenantId(),gaInvestorSubjectConfigReq.getOrganizationId());
        for (String subjectId:gaInvestorSubjectConfigReq.getSubjectIds()) {
            GaInvestorSubjectConfig investorSubjectConfig = BeanMapping.copy(gaInvestorSubjectConfigReq, GaInvestorSubjectConfig.class);
            investorSubjectConfig.setSubjectId(subjectId);
            try {
                investorSubjectConfig.setId(segmentService.getId(Constant.GA_INVESTOR_SUBJECT_CONFIG));
            } catch (SegmentException e) {
                logger.error("主键生成错误",e);
                throw new TxnException(ErrorMsgConstant.ERROR_ID);
            }
            investorSubjectConfig.setRecordVersionNumber(Constant.VERSION_NUMBER);
            extGaInvestorSubjectConfigMapper.insertSelective(investorSubjectConfig);
        }
        return BeanMapping.copy(gaInvestorSubjectConfigReq, GaInvestorSubjectConfigRes.class);
    }

    @Override
    public Boolean removeByInvestorIdAndTenId(String investorId, String tenantId,String orgId) throws TxnException {
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(investorId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        int i = extGaInvestorSubjectConfigMapper.deleteByInvestorIdAndTenId(investorId, tenantId,orgId);
        return i > 0;
    }

    @Override
    public GaInvestorSubjectConfigRes findByTenIdAndInvestorId(String tenantId, String investorId,String orgId) throws TxnException {
        GaInvestorSubjectConfigRes res;
        if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(investorId)) {
            logger.error(TxnRespCode.NOT_EMPTY.getMsg());
            throw new TxnException(TxnRespCode.NOT_EMPTY.getMsg());
        }
        List<GaInvestorSubjectConfig> gaInvestorSubjectConfigList = extGaInvestorSubjectConfigMapper.selectByTenIdAndInvestorIdAndOrgId(tenantId, investorId,orgId);
        List<String> subjectIds=new ArrayList<>();
        for (GaInvestorSubjectConfig gaSubjectConfig:gaInvestorSubjectConfigList) {
            subjectIds.add(gaSubjectConfig.getSubjectId());
        }
        GaInvestorSubjectConfig gaInvestorSubjectConfig=new GaInvestorSubjectConfig();
        if(CollectionUtils.isNotEmpty(gaInvestorSubjectConfigList)){
            gaInvestorSubjectConfig=gaInvestorSubjectConfigList.get(0);
        }
        res=BeanMapping.copy(gaInvestorSubjectConfig,GaInvestorSubjectConfigRes.class);
        res.setSubjectIds(subjectIds);
        return res;
    }
}
